package feeinterface.dao;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ulwx.database.DataBaseSet;
import com.ulwx.tool.CTime;
import com.ulwx.tool.DateTime;
import com.ulwx.tool.DbUtils;
import com.ulwx.type.TInteger;

import feeinterface.modeldb.ChannelUserMonthConsume;
/**
 * 用户使用通道月消费表
 * 
 * @author 黄杰
 * 
 */
public class ChannelUserMonthConsumeDao {
	/**
	 * 
	 */
	private static final Logger log = Logger
			.getLogger(ChannelUserMonthConsumeDao.class);

	/**
	 * 增加用户使用通道月消费记录
	 * 
	 * @param bi
	 * @throws Exception
	 */
	public static void InsertOrUpdate(ChannelUserMonthConsume bi) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("insert into ChannelUserMonthConsume");
		sb.append("(IMSI,ProvinceID,ChannelID,ServiceCoID,StatMonth,UseCnt,LastUpdateTime," +
				"FeeTotal,NoMoChannelFee,DoubleFee)");
		sb.append("values(?,?,?,?,?,?,?,?,?,?)");
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, bi.getIMSI());
		map.put(2, bi.getProvinceID());
		map.put(3, bi.getChannelID());
		map.put(4, bi.getServiceCoID());
		map.put(5, bi.getStatMonth());
		map.put(6, bi.getUseCnt());
		map.put(7, bi.getLastUpdateTime());
		map.put(8, bi.getFeeTotal());
		map.put(9, bi.getNoMoChannelFee());
		map.put(10, bi.getDoubleFee());

		try {
			DbUtils.executeBindUpdate(null, sb.toString(), map);
		} catch (Exception e) {
			log.error("", e);
		}
	}
	
	
	public static int getChannelUserMonthFee(String imsi,Integer channelID, TInteger noMoFee,
			TInteger doubleFee){
		
		String sql="select sum(FeeTotal) as fee,sum(DoubleFee) as DoubleFee," +
				"sum(NoMoChannelFee) as NoMoChannelFee   from ChannelUserMonthConsume" +
				" where IMSI=? and StatMonth=? and ChannelID=?";
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, imsi);
		DateTime dt=new DateTime();
		dt.setDay(1);
		map.put(2, new java.sql.Date(dt.getTimeInMillis()));
		map.put(3, channelID);
		try{
			DataBaseSet rs=DbUtils.doCachedQuery(null, sql, map);
			if(rs.next()){
				noMoFee.setValue(rs.getInt("NoMoChannelFee"));
				doubleFee.setValue(rs.getInt("DoubleFee"));
				
				return rs.getInt("fee");
			}
			return 0;
		}catch(Exception e){
			log.error("",e);
		}
		noMoFee.setValue(0);
		doubleFee.setValue(0);
		return 0;
	}
	public static void main(String[] args) {
		System.out.println(getChannelUserMonthFee("460026172066057",10000,new TInteger(0),new TInteger(0)));
	}
}
